import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _isFunction from "lodash/isFunction";
import _isString from "lodash/isString";
import _split from "lodash/split";

var _class;

/**
 * 工作区 Header
 *
 * @date: 2018-6-30

 */
import React, { Component } from 'react';
import { withRouter } from 'dva/router';
import { Tooltip, Icon, Modal } from 'antd';
import intl from "../../utils/intl";

var PageHeader = withRouter(_class = /*#__PURE__*/function (_Component) {
  _inherits(PageHeader, _Component);

  var _super = _createSuper(PageHeader);

  function PageHeader() {
    var _this;

    _classCallCheck(this, PageHeader);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));

    _this.onBackBtnClick = function () {
      var _this$props = _this.props,
          backPath = _this$props.backPath,
          history = _this$props.history,
          isChange = _this$props.isChange,
          onBack = _this$props.onBack;

      if (_isString(backPath)) {
        if (isChange) {
          Modal.confirm({
            title: intl.get('cpaas.common.message.confirm.giveUpTip').d('你有修改未保存，是否确认离开？'),
            onOk: function onOk() {
              _this.linkToChange(_this.props.backPath);

              if (_isFunction(onBack)) {
                onBack();
              }
            }
          });
        } else {
          _this.linkToChange(_this.props.backPath);

          if (_isFunction(onBack)) {
            onBack();
          }
        }
      } else {
        history.goBack();
      }
    };

    _this.linkToChange = function (url) {
      var history = _this.props.history; // const newUrl = `${url}${url.indexOf('?') === -1 ? '?' : '&'}_back=1`;
      // history.push(newUrl);

      var _split2 = _split(url, '?'),
          _split3 = _slicedToArray(_split2, 2),
          pathname = _split3[0],
          search = _split3[1];

      history.push({
        pathname: pathname,
        search: search,
        state: {
          _back: -1
        }
      });
    };

    return _this;
  }

  _createClass(PageHeader, [{
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
          title = _this$props2.title,
          backPath = _this$props2.backPath,
          _this$props2$backTool = _this$props2.backTooltip,
          backTooltip = _this$props2$backTool === void 0 ? intl.get('cpaas.common.button.back').d('返回') : _this$props2$backTool,
          children = _this$props2.children;
      var backBtn = '';

      if (backPath) {
        backBtn = /*#__PURE__*/React.createElement("div", {
          key: "page-head-back-btn",
          className: "page-head-back-btn"
        }, /*#__PURE__*/React.createElement(Tooltip, {
          title: backTooltip,
          placement: "bottom",
          getTooltipContainer: function getTooltipContainer(that) {
            return that;
          }
        }, /*#__PURE__*/React.createElement(Icon, {
          type: "arrow-left",
          className: "back-btn",
          onClick: this.onBackBtnClick
        })));
      }

      return /*#__PURE__*/React.createElement("div", {
        className: "page-head"
      }, backBtn, title && /*#__PURE__*/React.createElement("span", {
        key: "page-head-title",
        className: "page-head-title"
      }, title), /*#__PURE__*/React.createElement("div", {
        key: "page-head-operator",
        className: "page-head-operator"
      }, children));
    }
  }]);

  return PageHeader;
}(Component)) || _class;

export { PageHeader as default };